package com.s59.rabc.controller;

import com.mysql.cj.callback.UsernameCallback;
import com.s59.rabc.commen.LoginException;
import com.s59.rabc.commen.Dta;
import com.s59.rabc.commen.Token;
import com.s59.rabc.service.ILoginService;
import com.s59.rabc.entity.vo.LoginVo;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class LoginController {
    @Autowired
    private ILoginService loginService;

//    @PostMapping("/login")
//    public Dta login(@RequestBody LoginVo loginVo){
//        try {
//            Token token = loginService.login(loginVo);
//            return Dta.ok(token);
//        } catch (LoginException e) {
//            return Dta.err(e.getMessage());
//        }
//    }

//    @PostMapping("/login")
//    public Dta login(@RequestBody LoginVo loginVo){
//        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());
//        try {
//            SecurityUtils.getSubject().login(usernamePasswordToken);
//            return Dta.ok();
//        }catch (Exception e){
//            return Dta.err(e.getMessage());
//        }
//    }

    @PostMapping("/login")
    public Dta login(@RequestBody LoginVo loginVo){
        Token token = loginService.ssLogin(loginVo);
        return Dta.ok(token);
    }

    @GetMapping("/logout")
    public Dta logout(@RequestHeader String token){
        loginService.logout(token);
        return Dta.ok();
    }
}
